﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BYUStock
{
    public partial class StockUpdater : Form
    {
        public StockUpdater()
        {
            InitializeComponent();
            ParseStockHtml_DFCF1();
        }

        void ParseStockHtml_THS1()
        {
            var connStr = "Data Source =" + AppDomain.CurrentDomain.BaseDirectory + "stock_base.db;";
            using (var conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                var sql = $"select code from StockBaseInfo";
                using (var cmd = new SQLiteCommand(sql))
                {
                    cmd.Connection = conn;
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var code = reader.GetString(0);
                        if (code.StartsWith("6") || code.StartsWith("3"))
                        {
                            //string uriSOHU = $"https://q.stock.sohu.com/cn/{code}.shtml";
                            //var uriTHS = $"https://stockpage.10jqka.com.cn/600000/company/#detail";
                            var uriTHS2 = $"https://q.10jqka.com.cn/";
                            var content = Global.DownloadHtmlContent(uriTHS2, "StockInfos", code, "GB2312");//UTF-8
                            Console.WriteLine(content);
                            var codeMatchs = Regex.Matches(content, @"_blank\W>\d\d\d\d\d\d</a");
                            var mv_pe_Matchs = Regex.Matches(content, @"<td>\S*</td>\s*<td>\S*</td>\s*</tr>");
                            for (int i = 0; i < codeMatchs.Count; i++)
                            {
                                var stockCode = codeMatchs[i].Value.Split('>')[1].Substring(0, 6);
                                var mv_pe_matchs = Regex.Matches(mv_pe_Matchs[i].Value, @">\S*<");
                                var marketValue = mv_pe_matchs[0].Value.Replace(">", "").Replace("<", "");
                                var pe = mv_pe_matchs[1].Value.Replace(">", "").Replace("<", "");
                            }
                        }
                    }
                }
            }
        }
        void ParseStockHtml_THS2()
        {
            var connStr = "Data Source =" + AppDomain.CurrentDomain.BaseDirectory + "stock_base.db;";
            using (var conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                var sql = $"select code from StockBaseInfo";
                using (var cmd = new SQLiteCommand(sql))
                {
                    cmd.Connection = conn;
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var code = reader.GetString(0);
                        if (code.StartsWith("6") || code.StartsWith("3"))
                        {
                            //string uriSOHU = $"https://q.stock.sohu.com/cn/{code}.shtml";
                            //var uriTHS = $"https://stockpage.10jqka.com.cn/600000/company/#detail";
                            var uriTHS2 = $"https://stockpage.10jqka.com.cn/834765/";
                            var content = Global.DownloadHtmlContent(uriTHS2, "StockInfos", code, "UTF-8");//UTF-8
                            Console.WriteLine(content);
                            var codeMatchs = Regex.Matches(content, @"_blank\W>\d\d\d\d\d\d</a");
                            var mv_pe_Matchs = Regex.Matches(content, @"<td>\S*</td>\s*<td>\S*</td>\s*</tr>");
                            for (int i = 0; i < codeMatchs.Count; i++)
                            {
                                var stockCode = codeMatchs[i].Value.Split('>')[1].Substring(0, 6);
                                var mv_pe_matchs = Regex.Matches(mv_pe_Matchs[i].Value, @">\S*<");
                                var marketValue = mv_pe_matchs[0].Value.Replace(">", "").Replace("<", "");
                                var pe = mv_pe_matchs[1].Value.Replace(">", "").Replace("<", "");
                            }
                        }
                    }
                }
            }
        }
        int succs = 0;
        int fails = 0;
        void ParseStockHtml_DFCF1()
        {
            var connStr = "Data Source =" + AppDomain.CurrentDomain.BaseDirectory + "stock_base.db;";
            using (var conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                var sql = $"select code from StockBaseInfo";
                using (var cmd = new SQLiteCommand(sql))
                {
                    cmd.Connection = conn;
                    var reader = cmd.ExecuteReader();
                    using (var cmdUpdate = new SQLiteCommand())
                    {
                        cmdUpdate.Connection = conn;
                        while (reader.Read())
                        {
                            var code = reader.GetString(0);
                            var sqlExsit = $"select JG from StockBaseInfo where code = '{code}';";
                            cmdUpdate.CommandText = sqlExsit;
                            var jg = cmdUpdate.ExecuteScalar();
                            if (jg == null || !string.IsNullOrEmpty(jg.ToString())) continue;
                            if (!code.StartsWith("6") && !code.StartsWith("0")) continue;
                            var exc = code.StartsWith("6") ? "sh" : "sz";
                            var uriDFCF1 = $"https://eniu.com/gu/{exc}{code}#";
                            var content = Global.DownloadHtmlContent(uriDFCF1, "StockInfos", code, "UTF-8");//UTF-8
                            Console.WriteLine(content);
                            var mt_JG = Regex.Match(content, @"价\s格\W\s\S*>\S*\s\S<");
                            if (string.IsNullOrEmpty(mt_JG.Value)) continue;
                            var mt_SZ = Regex.Match(content, @"市\s值\W\s\S*>\S*\s\S<");
                            var mt_SYL = Regex.Match(content, @"市盈率\W\s\S*>\S*</");
                            var mt_SJL = Regex.Match(content, @"市净率\W\s\S*>\S*</");
                            var mt_SXL = Regex.Match(content, @"市销率\W\s\S*>\S*</");
                            var mt_GXL = Regex.Match(content, @"股息率\W\s\S*>\S*</");

                            var mt_FZL = Regex.Match(content, @"Box负债率\W>\S*</");
                            var mt_XJL = Regex.Match(content, @"Box现金流\W>\S*</");
                            var mt_MLL = Regex.Match(content, @"Box毛利率\W>\S*</");
                            var mt_PXL = Regex.Match(content, @"Box派息率\W>\S*</");
                            var mt_ZYS = Regex.Match(content, @"Box营\s收\W>\S*\s\S<");
                            var mt_JLR = Regex.Match(content, @"Box净利润\W>\S*\s\S<");

                            var JG = mt_JG.Value.Split('>')[1];
                            var SZ = mt_SZ.Value.Split('>')[1];
                            var SYL = mt_SYL.Value.Split('>')[1];
                            var SJL = mt_SJL.Value.Split('>')[1];
                            var SXL = mt_SXL.Value.Split('>')[1];
                            var GXL = mt_GXL.Value.Split('>')[1];
                            var FZL = mt_FZL.Value.Split('>')[1];
                            var XJL = mt_XJL.Value.Split('>')[1];
                            var MLL = mt_MLL.Value.Split('>')[1];
                            var PXL = mt_PXL.Value.Split('>')[1];
                            var ZYS = mt_ZYS.Value.Split('>')[1];
                            var JLR = mt_JLR.Value.Split('>')[1];

                            JG = JG.Substring(0, JG.IndexOf('<'));
                            SZ = SZ.Substring(0, SZ.IndexOf('<'));
                            SYL = SYL.Substring(0, SYL.IndexOf('<'));
                            SJL = SJL.Substring(0, SJL.IndexOf('<'));
                            SXL = SXL.Substring(0, SXL.IndexOf('<'));
                            GXL = GXL.Substring(0, GXL.IndexOf('<'));
                            FZL = FZL.Substring(0, FZL.IndexOf('<'));
                            XJL = XJL.Substring(0, XJL.IndexOf('<'));
                            MLL = MLL.Substring(0, MLL.IndexOf('<'));
                            PXL = PXL.Substring(0, PXL.IndexOf('<'));
                            ZYS = ZYS.Substring(0, ZYS.IndexOf('<'));
                            JLR = JLR.Substring(0, JLR.IndexOf('<'));

                            var sqlUpdate = $"update StockBaseInfo set " +
                                            $"JG='{JG}',SZ='{SZ}',SYL='{SYL}',SJL='{SJL}',SXL='{SXL}',GXL='{GXL}'," +
                                            $"FZL='{FZL}',XJL='{XJL}',MLL='{MLL}',PXL='{PXL}',ZYS='{ZYS}',JLR='{JLR}' " +
                                            $"where code ='{code}';";
                            cmdUpdate.CommandText = sqlUpdate;
                            var result = cmdUpdate.ExecuteNonQuery();
                            if (result == 1)
                            {
                                succs++;
                            }
                            else
                            {
                                fails++;
                            }

                        }
                    }
                }
            }
        }
    }
}
